/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import biz.AccountBO;
import entity.Account;
import java.sql.SQLException;
import java.util.Arrays;
import javax.swing.JOptionPane;

/**
 *
 * @author Administrator
 */
public class PanelChangePass extends javax.swing.JPanel {

    /**
     * Creates new form PanelChangePass
     */
    private String username;

    public PanelChangePass(String username) {
        initComponents();
        this.username = username;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlBody = new javax.swing.JPanel();
        lblCurPass = new javax.swing.JLabel();
        lblNewPass = new javax.swing.JLabel();
        lblConfPass = new javax.swing.JLabel();
        pswCurPass = new javax.swing.JPasswordField();
        pwsNewPass = new javax.swing.JPasswordField();
        pwsConfPass = new javax.swing.JPasswordField();
        filler1 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(32767, 32767));
        pnlControlBtn = new javax.swing.JPanel();
        btnChange = new javax.swing.JButton();

        setBackground(new java.awt.Color(185, 192, 14));
        setToolTipText("");

        pnlBody.setBackground(new java.awt.Color(185, 192, 14));

        lblCurPass.setText("Current Password");

        lblNewPass.setText("New Password");

        lblConfPass.setText("Confirm Password");

        pwsConfPass.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pwsConfPassActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlBodyLayout = new javax.swing.GroupLayout(pnlBody);
        pnlBody.setLayout(pnlBodyLayout);
        pnlBodyLayout.setHorizontalGroup(
            pnlBodyLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
            .addGroup(pnlBodyLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(pnlBodyLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(filler1, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(pnlBodyLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(pnlBodyLayout.createSequentialGroup()
                            .addComponent(lblCurPass, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(pswCurPass, javax.swing.GroupLayout.DEFAULT_SIZE, 231, Short.MAX_VALUE))
                        .addGroup(pnlBodyLayout.createSequentialGroup()
                            .addGroup(pnlBodyLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(lblNewPass, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(lblConfPass, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(pnlBodyLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(pwsConfPass)
                                .addComponent(pwsNewPass))))
                    .addContainerGap()))
        );
        pnlBodyLayout.setVerticalGroup(
            pnlBodyLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 264, Short.MAX_VALUE)
            .addGroup(pnlBodyLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(pnlBodyLayout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(pnlBodyLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(pnlBodyLayout.createSequentialGroup()
                            .addComponent(filler1, javax.swing.GroupLayout.DEFAULT_SIZE, 211, Short.MAX_VALUE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 42, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(pnlBodyLayout.createSequentialGroup()
                            .addGroup(pnlBodyLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(lblCurPass)
                                .addComponent(pswCurPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGap(18, 18, 18)
                            .addGroup(pnlBodyLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(lblNewPass)
                                .addComponent(pwsNewPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGap(18, 18, 18)
                            .addGroup(pnlBodyLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(lblConfPass)
                                .addComponent(pwsConfPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addContainerGap()))
        );

        pnlControlBtn.setBackground(new java.awt.Color(225, 156, 24));

        btnChange.setBackground(new java.awt.Color(0, 0, 0));
        btnChange.setForeground(new java.awt.Color(255, 255, 255));
        btnChange.setText("Change");
        btnChange.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnChangeActionPerformed(evt);
            }
        });
        pnlControlBtn.add(btnChange);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnlControlBtn, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
            .addComponent(pnlBody, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(pnlBody, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlControlBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void pwsConfPassActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pwsConfPassActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_pwsConfPassActionPerformed

    private void btnChangeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnChangeActionPerformed
        try {
            // TODO add your handling code here:
            if (new AccountBO().checkPassword(username, new String(pswCurPass.getPassword())) == null) {
                JOptionPane.showMessageDialog(this, "Wrong password");
                return;
            }
            if (Arrays.equals(pwsConfPass.getPassword(), pwsNewPass.getPassword()) == false) {
                JOptionPane.showMessageDialog(this, "Password not match");
                return;
            }
            int i = JOptionPane.showConfirmDialog(this, "Do you want to do this?", "Comfirm Dialog ", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
            if (i == 0) // neu nguoi dung chon "Yes"
            {
                new AccountBO().editAccount(new Account(username, new String(pwsNewPass.getPassword())));
                JOptionPane.showMessageDialog(this, "Approve successful", "Message Dialog Demo", JOptionPane.INFORMATION_MESSAGE);
            }
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, "Database Connection Error", "Runtime Error", JOptionPane.ERROR_MESSAGE);
        }

    }//GEN-LAST:event_btnChangeActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnChange;
    private javax.swing.Box.Filler filler1;
    private javax.swing.JLabel lblConfPass;
    private javax.swing.JLabel lblCurPass;
    private javax.swing.JLabel lblNewPass;
    private javax.swing.JPanel pnlBody;
    private javax.swing.JPanel pnlControlBtn;
    private javax.swing.JPasswordField pswCurPass;
    private javax.swing.JPasswordField pwsConfPass;
    private javax.swing.JPasswordField pwsNewPass;
    // End of variables declaration//GEN-END:variables
}
